package com.example.shopping.controller;

import com.example.shopping.dto.CartItem;
import com.example.shopping.entity.Address;
import com.example.shopping.entity.User;
import com.example.shopping.service.AddressService;
import com.example.shopping.service.CartService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;


@Controller
public class OrderController {
    @Autowired
    private AddressService addressService;

    @Autowired
    private CartService cartService;
    
    @GetMapping("/order/add")
    public String orderadd(HttpSession session, Model model) {
        //获取登录用户收货地址以及购物车数据
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            return "redirect:/user/login";
        }
        Integer userId = user.getId();
        List<Address> addressList = addressService.getAddress(userId);
        model.addAttribute("addresses", addressList);
        List<CartItem> car = cartService.getCar(userId);
        model.addAttribute("carItems", car);
        //获取总价格
        Double totalPrice = cartService.getTotalPrice(userId);
        model.addAttribute("totalPrice", totalPrice);
        return "order_add";
    }

}
